# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libmariadbclient
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.4.5
pkgrel=1
pkgdesc="MariaDB client libraries (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://mariadb.org/"
msys2_repository_url="https://github.com/mariadb-corporation/mariadb-connector-c"
msys2_references=(
  'aur: '
  'archlinux: '
  "cpe: cpe:/a:mariadb:mariadb"
)
license=('spdx:LGPL-2.1-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
source=(https://mirror.rackspace.com/mariadb/connector-c-${pkgver}/mariadb-connector-c-${pkgver}-src.tar.gz{,.asc}
        001-mingw-build.patch
        003-gcc-fix-use_VA_ARGS.patch)
sha256sums=('b17e193816cb25c3364c2cc92a0ad3f1d0ad9f0f484dc76b8e7bdb5b50eac1a3'
            'SKIP'
            '64ad2b8c85fb369c20c1941c896c2a7d5202a8b7259e9e31922c8d0bfcbfd198'
            'fc08055e5d63e310e2658b15f6e1f00b59f2aad2dec37560c01954fac6af4a6e')
validpgpkeys=("177F4010FE56CA3336300305F1656F24C74CD1D8") #MariaDB Package Signing Key <package-signing-key@mariadb.org>

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}"/mariadb-connector-c-${pkgver}-src
  apply_patch_with_msg \
    001-mingw-build.patch \
    003-gcc-fix-use_VA_ARGS.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  # Embed plugins with STATIC option for path relocation issues (GH #19861)
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DINSTALL_LIBDIR=lib \
    "${_extra_config[@]}" \
    -DWITH_EXTERNAL_ZLIB=ON \
    -DWITH_MYSQLCOMPAT=OFF \
    -DWITH_SSL="SCHANNEL" \
    -DWITH_UNIT_TESTS=OFF \
    -DCLIENT_PLUGIN_AUTH_GSSAPI_CLIENT=STATIC \
    -DCLIENT_PLUGIN_DIALOG=STATIC \
    -DCLIENT_PLUGIN_REMOTE_IO=STATIC \
    -DCLIENT_PLUGIN_PVIO_NPIPE=STATIC \
    -DCLIENT_PLUGIN_PVIO_SHMEM=STATIC \
    -DCLIENT_PLUGIN_CLIENT_ED25519=STATIC \
    -DCLIENT_PLUGIN_CACHING_SHA2_PASSWORD=STATIC \
    -DCLIENT_PLUGIN_SHA256_PASSWORD=STATIC \
    -DCLIENT_PLUGIN_MYSQL_CLEAR_PASSWORD=STATIC \
    -DCLIENT_PLUGIN_MYSQL_OLD_PASSWORD=STATIC \
    -DCLIENT_PLUGIN_ZSTD=STATIC \
    ../mariadb-connector-c-${pkgver}-src

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  ln -s "${pkgdir}${MINGW_PREFIX}"/include/mariadb "${pkgdir}${MINGW_PREFIX}"/include/mysql

  if [ "${CARCH}" = "i686" ]; then
    rm -f "${pkgdir}"${MINGW_PREFIX}/lib/libmariadb.dll.a
    ${MINGW_PREFIX}/bin/gendef -a "${pkgdir}"${MINGW_PREFIX}/bin/libmariadb.dll
    ${MINGW_PREFIX}/bin/dlltool -k -d libmariadb.def -l "${pkgdir}"${MINGW_PREFIX}/lib/libmariadb.dll.a
  fi

  cp "${pkgdir}${MINGW_PREFIX}"/lib/{libmariadb,libmysqlclient}.dll.a
  cp "${pkgdir}${MINGW_PREFIX}"/lib/{libmariadb,libmysqlclient_r}.dll.a
  cp "${pkgdir}${MINGW_PREFIX}"/lib/{libmariadbclient,libmysqlclient}.a
  cp "${pkgdir}${MINGW_PREFIX}"/lib/{libmariadbclient,libmysqlclient_r}.a
  cp "${pkgdir}${MINGW_PREFIX}"/bin/{mariadb_config,mysql_config}.exe
  cp "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/{libmariadb,mysqlclient}.pc

  local _PRE_WIN="$(cygpath -m ${MINGW_PREFIX})"

  for pcfile in  "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${_PRE_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done

  install -Dm644 "${srcdir}/mariadb-connector-c-${pkgver}-src/COPYING.LIB" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LIB"
}
